package com.intelligentclass.controller;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.intelligentclass.mydata.MyJson;
import com.intelligentclass.utils.JWTUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
public class TokenController {

    @GetMapping("/api/updateToken")
    public ResponseEntity<?> updateToken(@RequestHeader Map<String, String> headers) {
        MyJson json = new MyJson();

        String token = headers.get("token");
        try{
            JWTUtil.verifyToken(token);
        }
        catch(Exception e){
            json.setMessage(e.getMessage());
            json.setStatus(400);
            json.addData("status", -1);
            return ResponseEntity.ok().body(json);
        }

        token = JWTUtil.updateToken(token);
        json.setMessage("Successfully updated token");
        json.setStatus(200);
        json.addData("status", 200);
        json.addData("token", token);
        return ResponseEntity.ok().body(json);
    }
}
